package org.lql.algo.codecrush.hot100.skill;

import java.util.Arrays;

/**
 * @author: liangqinglong
 * @date: 2025-10-13 23:21
 * @description: 75. 颜色分类 <a href="https://leetcode.cn/problems/sort-colors/description/?envType=study-plan-v2&envId=top-100-liked">...</a>
 **/
public class SortColors {

	public void sortColors(int[] nums) {
		int left = 0;
		int right = nums.length - 1;
		int index = 0;
		while (index <= right) {
			if (nums[index] == 0) {
				swap(nums, left, index);
				left++;
				index++;
			} else if (nums[index] == 1) {
				index++;
			} else {
				swap(nums, index, right);
				right--;
			}
		}
	}

	private void swap(int[] nums, int i, int j) {
		int temp = nums[i];
		nums[i] = nums[j];
		nums[j] = temp;
	}

	public static void main(String[] args) {
		SortColors sortColors = new SortColors();
		int[] nums = new int[]{2, 0, 2, 1, 1, 0};
		sortColors.sortColors(nums);
		System.out.println(Arrays.toString(nums));
	}
}
